import * as bornInsert from './born-insert/Handler';
import * as bornUpdate from './born-update/Handler';
import * as bornDelete from './born-delete/Handler';
import * as bornQuery from './born-query/Handler';
import * as bornQueryByYear from './born-query-by-year/Handler';
import * as bornComplex from './born-complex/Handler';
import * as Utils from './utils/Utils';
import { AuthWrapper } from './auth/AuthWrapper';

const TAG: string = 'cloudDB-->';
/**
 * client HttpRequest
 * @param event
 * insert-1: {"operation":"insert","params":{"id":3,"bornYear":2020,"bornName":"鼠"}}
 * insert-2: {"operation":"insert","params":{"bornYear":2020,"bornName":"鼠"}}
 * update: {"operation":"update","params":{"id":3,"bornYear":2020,"bornName":"鼠"}}
 * delete: {"operation":"delete","params":{"id_list": [1,2]}}
 * query: {"operation":"query","params":{}}
 * queryByYear: {"operation":"queryByYear","params":{"bornYear": 2008}}
 * queryComplex: {"operation":"queryComplex","params":{"bornYear":2018}}
 * @param context
 * @param callback
 * @param logger
 */
let myHandler = async function (event, context, callback, logger) {
  //const credential = Utils.getCredential(context, logger);
  let operation;
  let params;
  let token

  operation = event.body ? JSON.parse(event.body).operation : event.operation;
  params = event.body ? JSON.parse(event.body).params : event.params;
  token = event.body ? JSON.parse(event.body).token : event.token;

  logger.info("the operation is " + event.operation);
  logger.info("the params is " + JSON.stringify(params));
  logger.info("the token is " + JSON.stringify(token));

  if (token) { //如果你传了token,则说明对此次请求，你是希望进行权限身份验证的
    let credential = Utils.getCredential(context, logger);
    const authWrapper = new AuthWrapper(credential, logger);
    let promise = authWrapper.verifyToken(token);
    promise.then(
      res => {
        doneWork(operation, params, context, callback, logger);
      }
    ).catch((e) => {
      logger.error("verifyToken Failed. Err:" + JSON.stringify(e));
      callback({
        result: {
          code: -3, desc: "token verify failed."
        }
      })
    })
  } else {
    doneWork(operation, params, context, callback, logger);
  }

}

function doneWork(operation, params, context, callback, logger) {
  try {
    switch (operation) {
      case "insert":
        bornInsert.myHandler(params, context, callback, logger);
        break;
      case "update":
        bornUpdate.myHandler(params, context, callback, logger);
        break;
      case "delete":
        bornDelete.myHandler(params, context, callback, logger);
        break;
      case "query":
        bornQuery.myHandler(params, context, callback, logger);
        break;
      case "queryByYear":
        bornQueryByYear.myHandler(params, context, callback, logger);
        break;
      case "queryComplex":
        bornComplex.myHandler(params, context, callback, logger);
        break;
      default:
        callback({
          result: { code: -2, desc: "no such function" },
        });
    }
  } catch (e) {
    logger.error("call function is error:" + e);
    callback({
      result: { code: -1, desc: "call func failed" },
    });
  }
}

export { myHandler };